package com.gljx.web.controller.JournalController;

import com.gljx.web.Entity.*;
import com.gljx.web.service.DepartmentService;
import com.gljx.web.service.SubjectOneRegService;
import com.gljx.web.service.SubjectThreeRegService;
import com.gljx.web.service.SubjectTwoRegService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/")
public class ExamDetailJournalController {
	@Autowired
	private DepartmentService departmentService;
	
	@Autowired
	private SubjectOneRegService subjectOneRegService;
	
	@Autowired
	private SubjectTwoRegService subjectTwoRegService;
	
	@Autowired
	private SubjectThreeRegService subjectThreeRegService;
	
	static DecimalFormat df = new DecimalFormat("######0.00");
	
	/**
	 * 根据时间和科目查询考试结果报表信息
	 * @param request
	 * @param subject
	 * @param beginTime
	 * @param endTime
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/queryExamDetailJournalBySubject/look.action")
	public String selExamDetailJournalBySubject(HttpServletRequest request,String subject,String beginTime,String endTime)throws Exception{	
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");                                   //获取已经登录用户数据
		Department department=departmentService.queryAdminByFid(user.getFcompanyid());   //获取登录用户组织ID
		Calendar cale = Calendar.getInstance();// 得到一个Calendar的实例
		  		 cale.add(Calendar.MONTH, 0);
		  		 cale.set(Calendar.DAY_OF_MONTH, 1);
		Date resultDate = cale.getTime(); // 结果
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(beginTime == null || beginTime.equals("")){
			beginTime = sdf.format(resultDate);
		}
		if(endTime == null  || endTime.equals("")){
			endTime = sdf.format(new Date());
		}
		List<SubjectExamDetail> subjectExamDetailList =null;//存数据集合
		Map<String,List<SubjectExamDetail>> map=new HashMap<>();//存数据集合
        if(subject == null ){
        	 subject="科目一"; 
        }            
		if(subject.equals("科目一")){//查询科目一
			subjectExamDetailList=getSubjectOne(user.getFcompanyid(),beginTime,endTime);
		}else if(subject.equals("科目二")){//查询科目二
			subjectExamDetailList=getSubjectTwo(user.getFcompanyid(),beginTime,endTime);
		}else if(subject.equals("科目三")){//查询科目三
			subjectExamDetailList=getSubjectThree(user.getFcompanyid(),beginTime,endTime);
		}else{
			
			map.put("one", getSubjectOne(user.getFcompanyid(),beginTime,endTime));
			map.put("two", getSubjectTwo(user.getFcompanyid(),beginTime,endTime));
			map.put("three", getSubjectThree(user.getFcompanyid(),beginTime,endTime));
		}
		if(subject.equals("")){//如果科目等于""说明查询全部
			request.setAttribute("map", map);
		}else{//否则单科查询
			request.setAttribute("subjectExamDetailList", subjectExamDetailList);
		}		
		request.setAttribute("department", department);
		request.setAttribute("subject", subject);
		request.setAttribute("beginTime", beginTime);
		request.setAttribute("endTime", endTime);
		return "backHome/journaling/manageJournaling/examDetailJournaling/examDetailJournalingList";
	}
	/**
	 * 获取科目一统计数据
	 * @param fcompanyid
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public List<SubjectExamDetail> getSubjectOne(String fcompanyid, String beginTime, String endTime){
		List<SubjectExamDetail>  subjectExamDetailList=new ArrayList<SubjectExamDetail>();
		List<SubjectOneReg> subjectOneSumEnterPersonsList=null;
		//获取总报考人数
		subjectOneSumEnterPersonsList=subjectOneRegService.selSubjectOneSumEnterPersons(fcompanyid, beginTime, endTime);
		List<SubjectOneReg> subjectOneResultPassExamRateList=null;
		//获取考试及格人数 不及格人数 缺考人数
		subjectOneResultPassExamRateList=subjectOneRegService.selSubjectOneResultPassExamRate(fcompanyid, beginTime, endTime);					
		if(subjectOneSumEnterPersonsList !=null && subjectOneResultPassExamRateList !=null){			
			for (int i = 0; i < subjectOneSumEnterPersonsList.size(); i++) {
				for (int j = 0; j < subjectOneResultPassExamRateList.size(); j++) {
					//System.out.println((subjectOneSumEnterPersonsList.get(i).getExamDate()+" "+subjectOneResultPassExamRateList.get(j).getExamDate()));
					if(subjectOneSumEnterPersonsList.get(i).getExamDate()
							.equals(subjectOneResultPassExamRateList.get(j).getExamDate())){
						SubjectExamDetail subjectExamDetail =new SubjectExamDetail();
						//考试时间
						subjectExamDetail.setExamDate(subjectOneSumEnterPersonsList.get(i).getExamDate());
						//给科目赋值
						subjectExamDetail.setSubject("科目一");										
						//获取总人数
						Long sum=subjectOneSumEnterPersonsList.get(i).getSumEnterPersons();
						subjectExamDetail.setSumEnterPersons(sum);
						//通过考试的人数
						Long pass=subjectOneResultPassExamRateList.get(j).getPassExamPersons();
						subjectExamDetail.setPassExamPersons(pass);
						//获取其不通过考试人数
						Long nopass=subjectOneResultPassExamRateList.get(j).getNoPassExamPersons();
						subjectExamDetail.setNoPassExamPersons(nopass);
						//获取缺考人数
						Long nojoin=subjectOneResultPassExamRateList.get(j).getNoJoinExamPersons();
						subjectExamDetail.setNoJoinExamPersons(nojoin);							
						//计算及格率
						if(pass !=null && sum!=null){
							subjectExamDetail.setPassExamRate(df.format((float) pass / (float) sum *100.00F));
						}
						subjectExamDetailList.add(subjectExamDetail);			
					}
					
				}					
			}								
		}
		return subjectExamDetailList;
	}
	/**
	 * 获取科目二统计数据
	 * @param fcompanyid
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public List<SubjectExamDetail> getSubjectTwo(String fcompanyid, String beginTime, String endTime){
		List<SubjectExamDetail>  subjectExamDetailList=new ArrayList<SubjectExamDetail>();
		List<SubjectTwoReg> subjectTwoSumEnterPersonsList=null;
		//获取总报考人数
		subjectTwoSumEnterPersonsList=subjectTwoRegService.selSubjectTwoSumEnterPersons(fcompanyid, beginTime, endTime);
		List<SubjectTwoReg> subjectTwoResultPassExamRateList=null;
		//获取考试及格人数 不及格人数 缺考人数
		subjectTwoResultPassExamRateList=subjectTwoRegService.selSubjectTwoResultPassExamRate(fcompanyid, beginTime, endTime);					
		if(subjectTwoSumEnterPersonsList !=null && subjectTwoResultPassExamRateList !=null){	
			for (int i = 0; i < subjectTwoSumEnterPersonsList.size(); i++) {
				for (int j = 0; j < subjectTwoResultPassExamRateList.size(); j++) {
					//System.out.println((subjectOneSumEnterPersonsList.get(i).getExamDate()+" "+subjectOneResultPassExamRateList.get(j).getExamDate()));
					if(subjectTwoSumEnterPersonsList.get(i).getExamDate()
							.equals(subjectTwoResultPassExamRateList.get(j).getExamDate())){
						SubjectExamDetail subjectExamDetail =new SubjectExamDetail();
						//考试时间
						subjectExamDetail.setExamDate(subjectTwoSumEnterPersonsList.get(i).getExamDate());
						//给科目赋值
						subjectExamDetail.setSubject("科目二");										
						//获取总人数
						Long sum=subjectTwoSumEnterPersonsList.get(i).getSumEnterPersons();
						subjectExamDetail.setSumEnterPersons(sum);
						//通过考试的人数
						Long pass=subjectTwoResultPassExamRateList.get(j).getPassExamPersons();
						subjectExamDetail.setPassExamPersons(pass);
						//获取其不通过考试人数
						Long nopass=subjectTwoResultPassExamRateList.get(j).getNoPassExamPersons();
						subjectExamDetail.setNoPassExamPersons(nopass);
						//获取缺考人数
						Long nojoin=subjectTwoResultPassExamRateList.get(j).getNoJoinExamPersons();
						subjectExamDetail.setNoJoinExamPersons(nojoin);							
						//计算及格率
						if(pass !=null && sum!=null){
							subjectExamDetail.setPassExamRate(df.format((float) pass / (float) sum *100.00F));
						}						
						subjectExamDetailList.add(subjectExamDetail);			
					}
					
				}					
			}							
		}
		return subjectExamDetailList;
	}
	/**
	 * 获取科目三统计数据
	 * @param fcompanyid
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public List<SubjectExamDetail> getSubjectThree(String fcompanyid, String beginTime, String endTime){
		List<SubjectExamDetail>  subjectExamDetailList=new ArrayList<SubjectExamDetail>();
		List<SubjectThreeReg> subjectThreeSumEnterPersonsList=null;
		//获取总报考人数
		subjectThreeSumEnterPersonsList=subjectThreeRegService.selSubjectThreeSumEnterPersons(fcompanyid, beginTime, endTime);
		List<SubjectThreeReg> subjectThreeResultPassExamRateList=null;
		//获取考试及格人数 不及格人数 缺考人数
		subjectThreeResultPassExamRateList=subjectThreeRegService.selSubjectThreeResultPassExamRate(fcompanyid, beginTime, endTime);					
		if(subjectThreeSumEnterPersonsList !=null && subjectThreeResultPassExamRateList !=null){			
			for (int i = 0; i < subjectThreeSumEnterPersonsList.size(); i++) {
				for (int j = 0; j < subjectThreeResultPassExamRateList.size(); j++) {
					//System.out.println((subjectOneSumEnterPersonsList.get(i).getExamDate()+" "+subjectOneResultPassExamRateList.get(j).getExamDate()));
					if(subjectThreeSumEnterPersonsList.get(i).getExamDate()
							.equals(subjectThreeResultPassExamRateList.get(j).getExamDate())){
						SubjectExamDetail subjectExamDetail =new SubjectExamDetail();
						//考试时间
						subjectExamDetail.setExamDate(subjectThreeSumEnterPersonsList.get(i).getExamDate());
						//给科目赋值
						subjectExamDetail.setSubject("科目三");										
						//获取总人数
						Long sum=subjectThreeSumEnterPersonsList.get(i).getSumEnterPersons();
						subjectExamDetail.setSumEnterPersons(sum);
						//通过考试的人数
						Long pass=subjectThreeResultPassExamRateList.get(j).getPassExamPersons();
						subjectExamDetail.setPassExamPersons(pass);
						//获取其不通过考试人数
						Long nopass=subjectThreeResultPassExamRateList.get(j).getNoPassExamPersons();
						subjectExamDetail.setNoPassExamPersons(nopass);
						//获取缺考人数
						Long nojoin=subjectThreeResultPassExamRateList.get(j).getNoJoinExamPersons();
						subjectExamDetail.setNoJoinExamPersons(nojoin);							
						//计算及格率
						if(pass !=null && sum!=null){
							subjectExamDetail.setPassExamRate(df.format((float) pass / (float) sum *100.00F));
						}
						subjectExamDetailList.add(subjectExamDetail);			
					}
					
				}					
			}								
		}
		return subjectExamDetailList;
	}
}
